package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:zhq
 * @Date: 2025/08/20 10:31
 * @Description:
 */
@Service
public class WechatServiceImpl implements WechatService {


    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.secret}")
    private String secret;

   /* @Override
    public String getOpenid(String code) {

        Map<String, Object> paramMap = getAppConfig();
        paramMap.put("js_code",code);
        String result = HttpUtil.get(REQUEST_URL, paramMap);
        JSONObject entries = JSONUtil.parseObj(result);
        if (ObjectUtil.isNotEmpty(entries.getInt("errcode"))){
            throw new RuntimeException(entries.getStr("errmsg"));
        }
        String openid = entries.getStr("openid");

        return openid;
    }*/

    @Override
    public String getOpenid(String code) {
        Map<String,Object> param = new HashMap<>();
        param.put("appid",appId);
        param.put("secret",secret);
        param.put("js_code",code);
        String result = HttpUtil.get(REQUEST_URL, param);
        if(StrUtil.isBlank(result)){
            throw new BaseException("获取openid失败");
        }
        String openid = JSONUtil.parseObj(result).getStr("openid");
        return openid;
    }

    private Map<String,Object> getAppConfig() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", appId);
        paramMap.put("secret", secret);
        return paramMap;
    }


    @Override
    public String getPhone(String detailCode) {
        String token = getToken();
        String url = PHONE_REQUEST_URL + token;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", detailCode);
        // 发起请求
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        // 是一个map
        JSONObject jsonObject = JSONUtil.parseObj(result);
        // 判断接口响应是否出错
        if(jsonObject.getInt("errcode") != 0) {
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }

        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    private String getToken() {

        Map<String, Object> paramMap = getAppConfig();
        // 发起请求
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        // 是一个map
        JSONObject jsonObject = JSONUtil.parseObj(result);
        // 判断接口响应是否出错
        if(ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }

        String token = jsonObject.getStr("access_token");
        return token;
    }

    private String getToken1(){
        Map<String, Object> paramMap = getAppConfig();
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        JSONObject entries = JSONUtil.parseObj(result);
        String accessToken = entries.getStr("access_token");
        return accessToken;


    }
}
